/**
 * 
 * @authors Riche
 * @date    2017-04-13 10:39:22
 * @version 1.2.0
 */
var ordertime = $.getCookie('ordertime');
if(ordertime===$.getUrlVar("order")){
    window.history.back();
}else{
    $.setCookie('ordertime',$.getUrlVar("order"));
}
var goodsCollection = $.getUrlVar("goodsCollection");
var buyNow = $.getUrlVar("buyNow");
try{
    var goodArr = goodsCollection.split(';');
    var goods = []; 
    for(var i in goodArr){
        var good = {
            goodId: (goodArr[i].split(','))[0],
            goodNum: (goodArr[i].split(','))[1]
        };
        goods.push(good);
    }
    //console.log(goods);
    var goodsId = [];
    var goodsNum = [];
    goods.forEach(function(item,index){
        goodsId.push(item.goodId);
        goodsNum.push(item.goodNum);
    });
    var goodsIDs = goodsId.join(',');   
}catch(e){
    console.error(e);
}

var info = new Vue({
    el: "#app",
    data: {
        tabs: 0,
        reqOut: false, //判断请求数据是否已完
        goodsInfo:[],
        goodsNum:goodsNum,
        placeInfo:[],
        placeList:{
          isLoading:true,
          pageSize:PAGESIZE,
          pageIndex:1
        },
        addrWay:false,
        post:false,
        wayText:'自提',
        selectPlace:false,
        title: '订单详情',
        selectedPlaceId:'',
        selectedPlaceName:'',
        receiveType:1 , //收货方式
        fill:{
            receiver:'',
            receiverPhone:'',
            Adrress:'',
            Remark:''
        },
        firstPlace:{
            id:Number,
            name:''
        }
    },
    created: function() {
        var dtd = $.Deferred();
        var _self = this;
        
        //订单详情
        var data = {
                goodsIDs: goodsIDs
        };
        data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'user_getGoodsListByIds', datatype: 'json', type: 'post', data: data }, function(data) {
            if (data.error_code == 0) {
                _self.goodsInfo = data.data;;
            } else {
                alert(data.msg);
            }
        });
        
        //myJS.init();
    },
    compiled: function() {
        var _self = this;
        _self.slides(_self.tabs);
    },
    computed: {
      totalPrice: function(){
        var totalPrice = 0;
        for(var i=0;i<this.goodsInfo.length;i++){
          totalPrice += (this.goodsInfo[i].price*goodsNum[i]);
        }
        return totalPrice;
      }
      
    },
    methods: {
        loadlist: function(tabs) {
            var _self = this;
            var vModal, target; //用来储存不同的tab页面对应的对象
            var url = 'user_selfTakeList';
            
            vModal = _self.placeList;
            target = _self.placeInfo;
            
            

            if (!vModal.isLoading) return;
            Common.ajaxCall({ url: url, datatype: 'json', type: 'post' }, function(data) {
                if (data.error_code == 0 && data.data.length > 0) {
                    if (vModal.pageIndex == 1) {
                        target.length = 0;
                    }
                    for (var i = 0; i < data.data.length; i++) {
                        target.push(data.data[i]);
                    }

                    _self.firstPlace.id = data.data[0].id;
                    _self.firstPlace.name = data.data[0].name;

                    _self.reqOut = false;

                    //实际数据与请求的数据量比较
                    if (data.data.length >= vModal.pageSize) {
                        myLoad.pullHide(); //加载后，把效果去掉
                    } else {
                        _self.reqOut = true;
                        myLoad.noDate(); //暂无数据
                        vModal.isLoading = false;
                    }
                } else {
                    _self.reqOut = true;
                    myLoad.noDate(); //暂无数据
                    vModal.isLoading = false;
                }

                vModal.pageIndex++;
            });

        },
        selectWay:function(opt){   //选择方式
          var _self = this;
          _self.addrWay = !_self.addrWay;
          if(opt){
            _self.wayText = opt;
            if(opt==='自提'){
                _self.post = false;
                _self.receiveType = 1; //自提
            }else{
                _self.post = true;
                _self.receiveType = 2; //邮寄
            }
          }
        },
        slides: function(index, e) {
            var _self = this;
            _self.tabs = index;

            if (e && typeof(e) != 'undefined') {
                $(e.currentTarget).addClass('on').siblings().removeClass('on');
            }
            
            //var str = '<img class="loading-img" src="images/loading.gif"></img>';
            //myLoad.pullLoad('quesCont', str);
            myLoad.pullHide();
            _self.quesList = []; //清空上一次请求vm储存的数据
            _self.reqOut = false;
            if (index === 2) { //调用分类接口
                _self.screen();
            } else { //调用问题列表
                var data = {
                    isLoading: true,
                    pageSize: PAGESIZE,
                    pageIndex: 1
                };
                _self = $.extend(_self, data);

                _self.loadlist(_self.tabs);
            }
            if (_self.tabs <= 3) {
                $.setCookie('askingtabs', _self.tabs); //保存最近的Tab
            }
        },
        loadPhoto: function() {
            // 比例
            myJS.imgScale('.j_1', '1', '1');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        decrease: function(e,index) {
            var _self = this;
            if($(e.target).hasClass('disable')) return;
            _self.cartInfo[index].selectcount--;
        },
        increase: function(e,index) {
            var _self = this;
            if($(e.target).hasClass('disable')) return;
            _self.cartInfo[index].selectcount++;
        },
        submitVerify:function(){
            var _self = this;

            if(_self.receiveType===1){ //自提
                var data = {
                    goods: goodsCollection,
                    receiveType: 1,
                    selfTake: _self.selectedPlaceName||_self.placeInfo[0].name,
                    Remark: _self.fill.Remark,
                    IsCart: buyNow=="true" ? 0: 1
                }
                _self.orderSubmit(data);
            }else{
                var data = {
                    goods: goodsCollection,
                    receiveType: 2,
                    receiver:_self.fill.receiver,
                    receiverPhone:_self.fill.receiverPhone,
                    Adrress:_self.fill.Adrress,
                    Remark: _self.fill.Remark,
                    IsCart: buyNow=="true" ? 0: 1
                }
                //way:1
                var config = {        //配置必填的项目
                    receiver:"收货人",
                    receiverPhone:"联系电话",
                    Adrress:"收货地址"
                }
                var flag = true;
                for(var item in data){
                    if(config.hasOwnProperty(item)){  //遍历提交的对象中是否有配置项
                        if(data[item]==''){
                            flag = false;
                            alert('请填写'+config[item]);
                        }
                    }
                }
                if(flag){
                    _self.orderSubmit(data);
                }
                
                //way:2
                /*
                var configarr = [
                {
                  name:"receiver",
                  value:"收货人"
                },
                {
                  name:"receiverPhone",
                  value:"联系电话"
                },
                {
                  name:"Adrress",
                  value:"收货地址"
                }];
                var text = '';
                function isfill(config){
                    text = config.value;
                    return  data[config.name]!= '';
                }
                if(configarr.every(isfill)){
                    //提交
                    console.log(123);
                }else{
                    console.log(text);
                }
                */
            }
        },
        orderSubmit: function(data) {  //提交订单
            Common.ajaxCall({ url: 'user_submitOrder', datatype: 'json', type: 'post', data: data }, function(data) {
                if (data.error_code == 0) {
                    alert({
                        text:'<span class="db padTB40 font26">'+data.msg+'</span>',
                        kind:2,
                        close:false,
                        onCancel:"继续购买",
                        onOk:"查看记录",
                        noEvent: function(){
                            window.location.href="shop.html";
                        },  
                        okEvent: function(){
                            $(window).on('popstate', function() {
                              var hashLocation = location.hash;
                              var hashSplit = hashLocation.split("#!/");
                              var hashName = hashSplit[1];

                              if (hashName !== '') {
                                var hash = window.location.hash;
                                if (hash === '') {
                                  alert('後退按鈕點擊');
                                }
                              }
                            });

                            window.location.href="exchange.html";
                        }
                    });
                } else {
                    alert(data.msg);
                }
            });

            /**/
        },
        toSelectPlace: function(id){  //选择自提点
            console.log(id);
            var _self = this;
            _self.selectPlace = true;
            _self.title = "自提地点";
            _self.selectedPlaceId = id;
        },
        selectedPlace:function(id,name){
            var _self = this;
            _self.selectPlace = false;
            _self.title = "订单详情";
            _self.selectedPlaceId = id;
            _self.selectedPlaceName = name;
        },
        back:function(){
          var _self = this;
          if(_self.selectPlace){
            _self.selectPlace = false;
            _self.title = "订单详情";
          }else{
            window.history.go(-1);
          }
          
        }
    },
    watch: {
        "goodsInfo": function() {
            this.loadPhoto();
        }
    }

});